#include "Main/stdafx.h"
#include "BRNNGameScence.h"
#include "Game/GameLib.h"
#include "BRNNPlayer.h"
#include "Game/Game/MissionWeiXin.h"
#include "BRNNExchangeLayer.h"

void BRNNGameScence::initPrivate()
{
	m_bEnterGetUserInfo = true;
}
void BRNNGameScence::defaultPrivateState()
{
	removeChildByTag(BRNN_GAME_DISMISS_LAYER);
}
void BRNNGameScence::OnSocketSubPrivateRoomInfo(CMD_GF_Private_Room_Info* pNetInfo)
{
	auto txt = (Text*)_layout->getChildByName("Sprite_table")->getChildByName("Text_deskNo");
	if (txt)
		txt->setString(utility::getScriptReplaceValue("PrivateTxt3", utility::toString(pNetInfo->dwRoomNum)));

	txt = (Text*)_layout->getChildByName("Text_deskInfo");
	if (txt)
	{
		std::string label1 = utility::getScriptString("world_private_rule3");
		std::string label2 = utility::toString((int)pNetInfo->dwPlayCout, "/", pNetInfo->dwPlayTotal);
		txt->setString(StringUtils::format(utility::getScriptString("world_private_table").c_str(),label1.c_str(),label2.c_str()));
	}

	setPlayCount(pNetInfo->dwPlayCout);
	bool bShowDismiss = false;
	if (pNetInfo->dwCreateUserID == m_pLocal->GetUserID())
	{
		if (pNetInfo->bStartGame || pNetInfo->dwPlayCout == 0)
		{
			bShowDismiss = true;
		}
	}
	if (pNetInfo->bStartGame)
	{
		bShowDismiss = true;
	}

	for (int i = 0; i < MAX_PLAYER; ++i)
	{
		BRNNPlayer* brPlayer = getPlayerByChairID(i);
		if (brPlayer->checkUserSitDown())
		{
			brPlayer->initUserMoney(pNetInfo->kUserScore[i]);
		}
		if (brPlayer->GetUserID() == pNetInfo->dwCreateUserID)
		{
			updateBankMoney(pNetInfo->kUserScore[i]);
		}
	}

	auto btn = (Button*)_layout->getChildByName("Button_addFriend");
	if (btn)
		btn->setVisible(!pNetInfo->bStartGame && pNetInfo->dwCreateUserID == m_pLocal->GetUserID());

	btn = (Button*)_layout->getChildByName("Button_dismiss");
	if (btn)
		btn->setVisible(bShowDismiss);
}
void BRNNGameScence::OnSocketSubPrivateEnd(CMD_GF_Private_End_Info* pNetInfo)
{
	defaultPrivateState();

	log("===========>CMD_GF_Private_End_Info<------------------");
	/*
	if (pNetInfo->lPlayerWinLose.size() != MAX_ROOM_PLAYER
		|| pNetInfo->lPlayerAction.size() != MAX_ROOM_PLAYER*MAX_PRIVATE_ACTION)
	{
		CCAssert(false,"pNetInfo->lPlayerWinLose.size() != MAX_HEAD_PLAYER");
		return;
	}

	Node* resultNode = CSLoader::createNode("resultLayer.csb");
	Director::getInstance()->getRunningScene()->addChild(resultNode, BRNN_GAME_PRIVATERESULT_LAYER);
	resultNode->setName("resultLayer");

	Button* btn = (Button*)resultNode->getChildByName("Button_back");
	btn->addClickEventListener([=](Ref* sender){
		Director::getInstance()->getRunningScene()->removeChildByName("resultLayer");
	});
	*/
}

void BRNNGameScence::OnSocketSubPrivateDiamondInfo(CMD_GR_REFRESH_DIAMOND* pNetInfo)
{
	int num = pNetInfo->lDiamond;
	CCLOG("zhuanshi +++++++++++++++++++++++++++++++++=  %d", num);
}

void BRNNGameScence::OnSocketSubPrivateDismissInfo(CMD_GF_Private_Dismiss_Info* pNetInfo)
{
	auto dismissPanel = (Node*) getChildByTag(BRNN_GAME_DISMISS_LAYER);
	if (!dismissPanel)
	{
		dismissPanel = CSLoader::createNode("dismissLayer.csb");
		dismissPanel->setScale(1.5);
		addChild(dismissPanel, BRNN_GAME_DISMISS_LAYER, BRNN_GAME_DISMISS_LAYER);
	}
	if (pNetInfo->dwDissUserCout == 0)
	{
		dismissPanel->runAction(Sequence::create(DelayTime::create(3.0f), RemoveSelf::create(), NULL));
		return;
	}
	int kChairID[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1};

	bool bShowSelfAction = true;
	int iIdex = 1;
	for (int i = 0; i < (int)pNetInfo->dwDissUserCout; i++)
	{
		std::string kName = "";
		GamePlayer* pPlayer = getPlayerByChairID(pNetInfo->dwDissChairID[i]);
		kChairID[pNetInfo->dwDissChairID[i]] = 0;
		if (pPlayer)
		{
			kName = StringUtils::format(utility::getScriptString("word_game_dismiss1").c_str(), pPlayer->GetChairID());
		}
		if (pPlayer == m_pLocal)
		{
			bShowSelfAction = false;
		}
		Text* nickName = (Text*)dismissPanel->getChildByName(StringUtils::format("Text_nickName%d", iIdex));
		nickName->setString(kName);
		nickName->setVisible(true);
		Text* dismissStatus = (Text*)dismissPanel->getChildByName(StringUtils::format("Text_dissStatus%d", iIdex));
		dismissStatus->setString("agree!");
		dismissStatus->setVisible(true);
		iIdex++;
	}
	for (int i = 0; i < (int)pNetInfo->dwNotAgreeUserCout; i++)
	{
		std::string kName = "";
		GamePlayer* pPlayer = getPlayerByChairID(pNetInfo->dwNotAgreeChairID[i]);
		kChairID[pNetInfo->dwNotAgreeChairID[i]] = 0;
		if (pPlayer)
		{
			kName = StringUtils::format(utility::getScriptString("word_game_dismiss1").c_str(), pPlayer->GetChairID());
		}
		if (pPlayer == m_pLocal)
		{
			bShowSelfAction = false;
		}
		Text* nickName = (Text*)dismissPanel->getChildByName(StringUtils::format("Text_nickName%d", iIdex));
		nickName->setString(kName);
		nickName->setVisible(true);
		Text* dismissStatus = (Text*)dismissPanel->getChildByName(StringUtils::format("Text_dissStatus%d", iIdex));
		dismissStatus->setString("not agree!");
		dismissStatus->setVisible(true);
		iIdex++;
	}
	for (int i = 0; i < MAX_PLAYER; i++)
	{
		if (kChairID[i] == 0)
		{
			continue;
		}
		Text* nickName = (Text*)dismissPanel->getChildByName(StringUtils::format("Text_nickName%d", iIdex));
		nickName->setString("");
		nickName->setVisible(true);
		Text* dismissStatus = (Text*)dismissPanel->getChildByName(StringUtils::format("Text_dissStatus%d", iIdex));
		dismissStatus->setString("");
		dismissStatus->setVisible(true);
		std::string kName = "";
		BRNNPlayer* pPlayer = getPlayerByChairID(i);
		if (!pPlayer || !pPlayer->checkUserSitDown())
		{
			continue;
		}
		if (pPlayer)
		{
			kName = StringUtils::format(utility::getScriptString("word_game_dismiss1").c_str(), pPlayer->GetChairID());
		}
		nickName->setString(kName);
		iIdex++;
	}

	auto btn = (Button*)dismissPanel->getChildByName("Button_cancel");
	btn->setVisible(bShowSelfAction);
	btn->addClickEventListener([=](Ref* pSender){
		CMD_GR_Dismiss_Private kNetInfo;
		kNetInfo.bDismiss = 0;
		CServerItem::get()->SendSocketData(MDM_GR_PRIVATE, SUB_GR_PRIVATE_DISMISS, &kNetInfo, sizeof(kNetInfo));
	});
	
	btn = (Button*)dismissPanel->getChildByName("Button_confirm");
	btn->setVisible(bShowSelfAction);
	btn->addClickEventListener([=](Ref* pSender){
		CMD_GR_Dismiss_Private kNetInfo;
		kNetInfo.bDismiss = 1;
		CServerItem::get()->SendSocketData(MDM_GR_PRIVATE, SUB_GR_PRIVATE_DISMISS, &kNetInfo, sizeof(kNetInfo));
	});
}
